﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Marmat.Forms.Design;

namespace Marmat.Forms.Design
{
    public partial class DatabaseTablesForm : Form
    {

        private readonly Marmat.Forms.Local.DataGrid _dataGrid;

        internal DatabaseTablesForm(Marmat.Forms.Local.DataGrid dataGrid)
        {
            InitializeComponent();
            this._dataGrid = dataGrid;
            Marmat.Forms.Design.DesignSupport.InitializeService(this._dataGrid);
        }

        private void DatabaseTablesForm_Load(object sender, EventArgs e)
        {
            Marmat.Forms.IDesignSupport design = null;
            var service = design.GetDataGridService();
            if (service != null)
            {
                ConnectionDescription[] connections = service.GetConnectionCollection();
                foreach (var connection in connections)
                {
                    this.comboBox1.Items.Add(connection.Name);
                }

                if (this.comboBox1.Items.Count > 0)
                {
                    if (string.IsNullOrEmpty(this._dataGrid.ConnectionName))
                        this.comboBox1.SelectedIndex = 0;
                    else
                        this.comboBox1.SelectedItem = this._dataGrid.ConnectionName;
                }
            }
        }

        private void ComboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            this.bDataGrid1.Rows.Clear();
            Marmat.Forms.IDesignSupport design = null;
            var service = design.GetDataGridService();
            if (service != null)
            {
                string connectionName = this.comboBox1.SelectedItem.ToString();
                try
                {
                    TableDescription[] tables = Marmat.Forms.AsyncHelper.Run(() => service.GetTableCollection(connectionName));
                    foreach (var table in tables)
                    {
                        Marmat.Forms.Views.DataGrid.Row row = this.bDataGrid1.AddNew();
                        row["Type"] = table.Group == "Tables" ? "数据" : "视图";
                        row["Name"] = table.Name;
                        row["Description"] = table.Description;
                    }
                    this.bDataGrid1.AcceptChanges();
                }
                catch (Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.Message);
                }
            }
        }

        /// <summary>
        /// 刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button2_Click(object sender, EventArgs e)
        {
            ComboBox1_SelectedValueChanged(comboBox1, EventArgs.Empty);
        }

        private void BGrid2_RowCellDoubleClick(object sender, Views.DataGrid.RowColEventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }
    }
}
